#!/usr/bin/python



from xpcom import components
import logging
logger = logging.getLogger("places_manager")



############################################################

class PlacesManager:
	
	def __init__(self):
		
		self.bmsvc = components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].getService(components.interfaces.nsINavBookmarksService)
		
		self.tagssvc = components.classes["@mozilla.org/browser/tagging-service;1"].getService(components.interfaces.nsITaggingService)
		
		self.ios = components.classes["@mozilla.org/network/io-service;1"].getService(components.interfaces.nsIIOService)

############################################################

	""" OLD! These methods are already programmed in some XPCOM components.
	def getMozillaTags(self):
		
		tagsFolder = self.bmsvc.tagsFolder
		parents = set([tagsFolder])
		return self.getAllChildrenOf(parents)
	
############################################################
	
	def getAllChildrenOf(self, parents):
		
		logger.debug("getAllChildrenOf(%s)" % parents)
		
		all_tags_ids = set()
		
		children = set()
		for parent in parents:
			logger.debug("parent = %s" % parent)
			childrenOfParent = self.getChildrenOf(parent)
			children |= childrenOfParent
		
		all_tags_ids |= children
		
		if children != set():
			all_tags_ids |= self.getAllChildrenOf(children)
		
		return all_tags_ids
	
############################################################
	
	def getChildrenOf(self, folder):
		
		logger.debug("getChildrenOf(%s)" % folder)
		
		children = set()
		index = 0
		
		while (True):
			
			try:
				# logger.debug("\tbmsvc.getIdForItemAt(%s, %s)" % (folder, index))
				child = self.bmsvc.getIdForItemAt(folder, index)
				children.add(child)
				index += 1
			except Exception, e:
				# logger.debug("\tNo more children (%s)" % e)
				break # while loop
		
		logger.debug("\tchildren(%s) = %s" % (folder, children))
		
		return children

############################################################

	def getItemInfo(self, itemId):
		
		title = self.bmsvc.getItemTitle(itemId)
		
		res = {
			"title": title,
			"dateAdded": self.bmsvc.getItemDateAdded(itemId),
			"lastModified": self.bmsvc.getItemLastModified(itemId),
			# "bookmarkURI": self.bmsvc.getBookmarkURI(itemId),
			"type": self.bmsvc.getItemType(itemId),
			"keyword": self.bmsvc.getKeywordForBookmark(itemId),
			# "URIs": self.tagssvc.getURIsForTag(title)
		}
		return res
	"""

############################################################

	def getAllFFTags(self):
		
		tags = self.tagssvc.allTags
		all_tags = set()
		
		for tag in tags:
			uris = map(lambda elt: elt.QueryInterface(components.interfaces.nsIURI).spec, self.tagssvc.getURIsForTag(tag))
			
			for uri in uris:
				
				bm_ids = self.bmsvc.getBookmarkIdsForURI(self.ios.newURI(uri, None, None))
				
				for bm_id in bm_ids:
					title = self.bmsvc.getItemTitle(bm_id)
					if title != "":
						all_tags.add( (uri, tag, title) )
						break # for bm_id in bm_idss
		
		return all_tags

############################################################

	def registerTag(self, uri, tag_label, title):
		
		nsIURI = self.ios.newURI(uri, None, None)
		
		if not self.bmsvc.isBookmarked(nsIURI):
			self.bmsvc.insertBookmark(self.bmsvc.unfiledBookmarksFolder, nsIURI, -1, title)
		
		self.tagssvc.tagURI(nsIURI, [tag_label])

############################################################

	def removeTag(self, uri, tag_label):
		
		nsIURI = self.ios.newURI(uri, None, None)
		
		self.tagssvc.untagURI(nsIURI, [tag_label])









